package com.ruoyi.app.framework.config;

import com.ruoyi.app.framework.interceptor.AuthenticationInterceptor;
import com.ruoyi.app.framework.interceptor.BusinessAuthInterceptor;
import com.ruoyi.app.framework.resolver.AuthMemberMethodArgumentResolver;
import com.ruoyi.app.framework.resolver.AuthUserMethodArgumentResolver;
import com.ruoyi.app.framework.resolver.ParseShopIdMethodArgumentResolver;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.Constants;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * 通用配置
 * 
 * @author ruoyi
 */
@Configuration
public class ResourcesConfig implements WebMvcConfigurer
{
    /**
     * 首页地址
     */
    @Value("${api.index}")
    private String indexUrl;

    @Bean
    //用户登录授权验证拦截器
    public AuthenticationInterceptor authenticationInterceptor() {
        return new AuthenticationInterceptor();
    }

    @Bean
    //商家登录授权验证拦截器
    public BusinessAuthInterceptor businessAuthInterceptor() {
        return new BusinessAuthInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(authenticationInterceptor())
                .addPathPatterns("/api/**"); // 拦截接口请求
        registry.addInterceptor(businessAuthInterceptor())
                .addPathPatterns("/business/**"); // 拦截商家接口请求
    }

    @Override
    //增加参数解析器
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(new ParseShopIdMethodArgumentResolver());
        argumentResolvers.add(new AuthMemberMethodArgumentResolver());
        argumentResolvers.add(new AuthUserMethodArgumentResolver());
    }

    /**
     * 默认首页的设置，当输入域名是可以自动跳转到默认指定的网页
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("forward:" + indexUrl);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry)
    {
        /** 本地文件上传路径 */
        registry.addResourceHandler(Constants.RESOURCE_PREFIX + "/**").addResourceLocations("file:" + RuoYiConfig.getProfile() + "/");

        /** swagger配置 */
        registry.addResourceHandler("/swagger-ui/**").addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/");
    }

}